package com.itheima.book.controller;

import com.itheima.book.interceptor.JwtClaimsConstant;
import com.itheima.book.interceptor.JwtUtil;
import com.itheima.book.pojo.dto.RegisterDTO;
import com.itheima.book.pojo.entity.User;
import com.itheima.book.pojo.vo.UserLoginVO;
import com.itheima.book.service.UserService;

import com.itheima.book.utils.result.Result;
import com.itheima.book.pojo.dto.UserLoginDto;
import com.itheima.book.utils.thirdparty.SmsComponent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itheima.book.interceptor.JwtProperties;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/api")
@Api(tags = "用户登录注册相关接口")
public class LoginOrSignupController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private SmsComponent smsComponent;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserLoginDto userLoginDto) {
        //1.用户进行登录
        log.info("用户名、密码登录:{}",userLoginDto);
        User user = userService.login(userLoginDto);

        //2.登录成功下发jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
        log.info("jwt令牌:{}",token);

        //3.响应
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 短信登录登录
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login/sms")
    @ApiOperation("短信登录")
    public Result loginSms(@RequestBody UserLoginDto userLoginDto) {
        //1.用户进行登录
        log.info("用户登录:{}",userLoginDto);
        User user = userService.loginsms(userLoginDto);

        //2.登录成功下发jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
        log.info("jwt令牌:{}",token);

        //3.响应
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 邮箱登录登录
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login/email")
    @ApiOperation("邮箱登录")
    public Result loginEmai(@RequestBody UserLoginDto userLoginDto) {
        //1.用户进行登录
        log.info("邮箱登录:{}",userLoginDto);
        User user = userService.loginemail(userLoginDto);

        //2.登录成功下发jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
        log.info("jwt令牌:{}",token);

        //3.响应
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody RegisterDTO registerDTO){
        log.info("用户注册:{}",registerDTO);
        userService.register(registerDTO);
        return Result.success();
    }
}
